package com.sl.core.engine.utils;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

@Slf4j
public class BeanUtil extends cn.hutool.core.bean.BeanUtil {

    public static Long getLong(Object bean, String... expression) {

        Long result = null;
        for (String s : expression) {
            result = getProperty(bean, s, Long.class, null);
            if (ObjectUtil.isNotNull(result)) {
                break;
            }
        }
        return result;
    }

    public static BigDecimal getDecimal(Object bean, String... expression) {
        BigDecimal result = null;
        for (String s : expression) {
            result = getProperty(bean, s, BigDecimal.class, null);
            if (ObjectUtil.isNotNull(result)) {
                break;
            }
        }
        return result;
    }

    public static String getStr(Object bean, String... expression) {
        String result = null;
        for (String s : expression) {
            result = getProperty(bean, s, String.class, null);
            if (StrUtil.isNotBlank(result)) {
                break;
            }
        }
        return result;
    }

    public static Date getDate(Object bean, String... expression) {
        Date result = null;
        for (String s : expression) {
            result = getProperty(bean, s, Date.class, null);
            if (ObjectUtil.isNotNull(result)) {
                break;
            }
        }
        return result;
    }

    public static Integer getInteger(Object bean, String... expression) {
        Integer result = null;
        for (String s : expression) {
            result = getProperty(bean, s, Integer.class, null);
            if (ObjectUtil.isNotNull(result)) {
                break;
            }
        }
        return result;
    }

    public static <T> T getProperty(Object bean, String expression, Class<T> tClass, T defaultValue) {


        Object property = getProperty(bean, expression);

        if (ObjectUtil.isNull(property)) {
            return null;
        }
        if (tClass.equals(Long.class)) {

            if (property instanceof String) {

                try {
                    property = Long.valueOf(property.toString());
                } catch (Exception e) {
                    try {
                        property = DateUtil.parse(property.toString(), DatePattern.NORM_DATETIME_PATTERN).getTime();
                    } catch (Exception ee) {
                        log.error(property + "转换失败", ee);
                    }
                }
            }
        } else if (tClass.equals(Date.class)) {
            if (property instanceof Long) {
                property = DateUtil.date((Long) property);
            } else if (property instanceof String) {
                try {
                    property = DateUtil.date(Long.valueOf(property.toString()));
                } catch (Exception e) {
                    try {
                        property = DateUtil.parse(property.toString(), DatePattern.NORM_DATETIME_PATTERN);
                    } catch (Exception ee) {
                        try {
                            property = DateUtil.parse(property.toString());
                        } catch (Exception eee) {
                            log.error("转换出错了", eee);
                        }
                    }
                }
            }

        }
        return Convert.convert(tClass, property, defaultValue);
    }

    public static <T> T convert(Object execute) {
        if(ObjectUtil.isNull(execute)){
            return null;
        }
        return (T) execute;
    }

    public static Boolean getBool(Object bean, String... expression) {
        return getBool(bean,false,expression);
    }

    public static Boolean getBool(Object bean, Boolean defaultValue,String... expression) {
        Boolean result = null;
        for (String s : expression) {
            result = getProperty(bean, s, Boolean.class, defaultValue);
            if (ObjectUtil.isNotNull(result)) {
                break;
            }
        }
        return result;
    }
        public static Map<String, Object> getMap(Map<String, Object> param, String... expression) {
        Map<String, Object> result = null;
        for (String s : expression) {
            result = getProperty(param, s, Map.class, null);
            if (ObjectUtil.isNotNull(result)) {
                break;
            }
        }
        return result;
    }
}
